package com.sbl.bajw.pc.service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.sbl.bajw.common.MyBatisDao;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.WebContext;
import com.sbl.bajw.mapper.OrderPushTModelDao;
import com.sbl.bajw.pc.model.OrderPushTModel;
import com.sbl.bajw.util.FileUpload;
import com.sbl.bajw.util.LogUtil;

@Service
public class OrderPushTModelService {

	@Resource
	private MyBatisDao gridDao;
	
	@Resource
	private OrderPushTModelDao dao;
	
	static Logger log;
	
	static{
		log = LogUtil.getLogger("OrderPush");
	}
	
	public String getOrderModelListPage(HashMap<String, Object> parm){
		String list = gridDao.selectPage(OrderPushTModelDao.class, parm, "getOrderModelList");
		return list;
	}
	
	public List<HashMap<String, Object>> getOrderModelList(HashMap<String, Object> parm){
		List<HashMap<String, Object>> list = dao.getOrderModelList(parm);
		return list;
	}
	
	public HashMap<String, Object> getModel(String id){
		HashMap<String, Object> model = dao.getModel(new BigDecimal(Integer.parseInt(id)));
		return model;
	}
	
	public void updateReadStatus(String id) {
		OrderPushTModel model = new OrderPushTModel();
		model.setId(id);
		model.setOrderReadStatus("1");
		dao.updateOrderPushTModel(model);
	}
	
	public void CompleteInstructions(String id, String positionX, String positionY, String note, String address) throws Exception{
		OrderPushTModel model = new OrderPushTModel();
			model.setId(id);
			model.setOrderNote(note);
			model.setOrderFinishX(positionX);
			model.setOrderFinishY(positionY);
			model.setOrderFinishAddress(address);
			model.setOrderFinishTime(new Timestamp(Calendar.getInstance().getTimeInMillis()));
			model.setOrderStatus("1");
			uploadFile(model);
			dao.updateOrderPushTModel(model);
	}
	
	public void insert(OrderPushTModel model) throws Exception{
		String useridsStr = model.getUserid();
		String[] userids = useridsStr.split(",");
		String equipmentcodes = RequestUnit.getString("equipmentcodes");
		for(int i=0;i<userids.length;i++){
			String orderId = dao.getid();
			model.setId(orderId);
			model.setUsername(userids[i]);
			model.setUserid(userids[i]);
			model.setOrderStatus("0");
			model.setCreatetime(new Timestamp(Calendar.getInstance().getTimeInMillis()));
			model.setOrderReadStatus("0");
			dao.insert(model);
		}
		
		if(equipmentcodes != null && !equipmentcodes.equals("")){
			insertToSendtask(model,equipmentcodes);
		}
	}
	
	public String getid(){
		String id = dao.getid();
		return id;
	}
	
	public void insertToSendtask(OrderPushTModel model,String equipmentcodes){
		if(equipmentcodes!=null && !equipmentcodes.equals("")){
			String[] equipmentcodeArray = equipmentcodes.split(",");
			
			HashMap<String, String> parm = new HashMap<String, String>();
			String taskId = dao.getSendTaskId();
			parm.put("taskid", taskId);
			parm.put("taskcontent", model.getOrderCode() +","+ model.getOrderTitle() +","+ model.getOrderContent());
			dao.insertToSendtask(parm);
			
			for(String equipmentcode : equipmentcodeArray){
				if(equipmentcode != null && !equipmentcode.equals("")){
					HashMap<String, String> parm2 = new HashMap<String, String>();
					parm2.put("taskid", taskId);
					parm2.put("equipmentcode", equipmentcode);
					dao.insertToSendNumber(parm2);
				}
			}
		}
	}
	
	public String uploadFile(OrderPushTModel model) throws Exception{
		String id = UUID.randomUUID().toString();
		
		FileItem item = RequestUnit.getFileItem("img");
		String resultPath = "";
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssS");
		
		if(item!=null)
		{
			String path = WebContext.getRequest().getSession().getServletContext().getInitParameter("UploadPath");
			String dataStr = sf.format(Calendar.getInstance().getTime());
			resultPath = "images/orderPush/"+FileUpload.saveFile(item, path+"/images/orderPush/",dataStr+"_img_"+id);
			log.info(id+"图片上传成功:"+resultPath);
			model.setOrderPhotourl(resultPath);
		}
		else{
			log.info(id+"没有图片数据");
		}
		
		FileItem record = RequestUnit.getFileItem("record");
		if(record!=null)
		{
			String path=WebContext.getRequest().getSession().getServletContext().getInitParameter("UploadPath");
			String dataStr = sf.format(Calendar.getInstance().getTime());
			resultPath = "record/orderPush/" + FileUpload.saveFile(record, path+"/record/orderPush/",dataStr+"_record_"+id);
			log.info(id+"录音上传成功:"+resultPath);
		}
		else{
			log.info(id+"没有录音数据");
		}
		return resultPath;
	}
}
